﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Module.DataAccess;
using Module.EntitiesInfo;


namespace Module.BusinessLogic
{
    public class QuestionBL
    {
        
        public static void Delete(int id)
        {
            try
            {
                new QuestionDB().Delete(id);
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public static int Insert(QuestionInfo info)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
                return new QuestionDB().Insert(info);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void Update(QuestionInfo info)
        {
            try
            {
                new QuestionDB().Update(info);
                DataCach.DataCach cache = new DataCach.DataCach();
                cache.RemoveAll();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static QuestionInfo GetInfo(int id)
        {
            try
            {
                QuestionInfo answerInfo;
                answerInfo = ObjectHelper.FillObject<QuestionInfo>(new QuestionDB().GetInfo(id));
                if (answerInfo != null)
                    return answerInfo;
                return null;                 
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static List<QuestionInfo> GetDataQuestion(int top)
        {
            try
            {
                DataCach.DataCach cache = new DataCach.DataCach();
                string strCache = top + "{AAC37EC4-B875-44B4-8DC7-49AF3F8CEDD4}";
                List<QuestionInfo> info=new List<QuestionInfo>();
                if (cache.GetCache(strCache) != null)
                {
                    info = (List<QuestionInfo>)(cache.GetCache(strCache));
                    return info;
                }
                info = ObjectHelper.FillCollection<QuestionInfo>(new QuestionDB().GetDataQuestion(top));
                if (info != null && info.Count > 0)
                {
                    cache.InsertCache(strCache, info, Convert.ToDouble(ConfigurationSettings.AppSettings["timeCache"].ToString()));
                }
                return info;                                  
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static int GetCount(string keysearch, int categoryId, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow)
        {
            try
            {
                return new QuestionDB().GetCount(keysearch, categoryId, starDatetime, endDatetime, beginRow, endRow);
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        public static DataTable Search(string keysearch, int categoryId, DateTime starDatetime, DateTime endDatetime, int beginRow, int endRow, string orderByColumn)
        {
            try
            {
                return new QuestionDB().Search(keysearch, categoryId, starDatetime, endDatetime, beginRow, endRow, orderByColumn);
            }
            catch (Exception ex)
            {
                throw;
            }

        }
               
    }
}

